<?php

namespace App\Http\Controllers\car;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class CarController extends Controller
{
	public function success(){
		return view('car.successtocar');
	}
    //购物车的搜索分页
    public function index(Request $request){
    	$where = [];
    	$hoppy = [];
        $user_id = session('homeuser')->id;
    	$db = DB::table('car')->where('user_id',$user_id);
    	if($request->has('name')){
    		$name = $request->input('name');
    		$db->where('name','like',"%{$name}%");
    		$where['name'] = $name;
    	}
    	$where['hoppy'] = $request->input('hoppy');
    	$list = $db->paginate(10);
    	return view('car.shopping',['list'=>$list,'where'=>$where,'hoppy'=>$hoppy]);
    }
    //删除购物车中的商品
    public function destroy($id){
    	$m = DB::table('car')->where('id',$id)->delete();
    	if($m>0){
    		return redirect('car/shopping');
    	}else{
    		return "删除失败";   	
    	}
    }
    //ajax计算用户选中的商品的总价
    public function post(Request $request){
    	$ck = $request->input('ck');
    	$ckrr = explode(',',$ck);
    	array_pop($ckrr);
    	$ck_price = array_sum($ckrr);
    	$nck = $request->input('nck');
    	$nckrr = explode(',',$nck);
    	array_pop($nckrr);
    	$nck_price = array_sum($nckrr);
    	$price = $ck_price-$nck_price;
    	echo json_encode($price);
    }
    public function chat(Request $request){
        $content = $request->input('content');
        $manager_id = 1;
            DB::table('chat')->insertGetId(
                ['manager_id' => $manager_id, 'content' => $content]
            );
        
        echo json_encode($content);
    }
    public function get_chat(Request $request){
        $data = DB::table('chat')->get();
        echo json_encode($data);
    }
    
}
